﻿using System.Windows.Forms;
using FSLib.App.SimpleUpdater;

namespace pinduoduo.Util
{
    public class AutoUpdate
    {
        public Updater updater;

        private static AutoUpdate autoUpdate = new AutoUpdate();

        private AutoUpdate() 
        {
            updater = Updater.CreateUpdaterInstance("http://106.12.151.125/download/update/{0}", "update.xml");
            //updater = Updater.CreateUpdaterInstance("http://127.0.0.1/file/{0}", "update.xml");
            //配置外部更新程序为强制结束进程模式
            updater.Context.AutoExitCurrentProcess = true;
            updater.Context.AutoKillProcesses = true;
            updater.Context.AutoEndProcessesWithinAppDir = true;
            updater.Context.EnableEmbedDialog = false;
        }

        public static AutoUpdate GetInstance() 
        {
            return autoUpdate;
        }

        public void Start()
        {
            updater.Error += (s, e) =>
            {
                MessageBox.Show("更新发生了错误：" + updater.Context.Exception.Message);
            };
            updater.UpdatesFound += (s, e) =>
            {
                //MessageBox.Show("发现了新版本：" + updater.Context.UpdateInfo.AppVersion); 
                updater.StartExternalUpdater();
            };
            updater.NoUpdatesFound += (s, e) =>
            {
                //Application.Run(new Login());
            };
            updater.MinmumVersionRequired += (s, e) =>
            {
                MessageBox.Show("当前版本过低无法使用自动更新！");
            };
   
            updater.BeginCheckUpdateInProcess();
            updater.EnsureNoUpdate();
        }
    }
}
